import 'package:flutter/material.dart';
import '../widgets/custom_dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  bool _notificationEnabled = true;
  bool _soundEnabled = true;
  bool _vibrationEnabled = true;

  /// 构建设置项
  Widget _buildSettingItem({
    required String title,
    String? subtitle,
    Widget? trailing,
    VoidCallback? onTap,
    bool showDivider = true,
    Color? titleColor,
  }) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        InkWell(
          onTap: onTap,
          child: Container(
            padding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 14,
            ),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        title,
                        style: TextStyle(
                          fontSize: 16,
                          color: titleColor ?? Colors.grey[800],
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      if (subtitle != null) ...[
                        const SizedBox(height: 4),
                        Text(
                          subtitle,
                          style: TextStyle(
                            fontSize: 13,
                            color: Colors.grey[500],
                          ),
                        ),
                      ],
                    ],
                  ),
                ),
                const SizedBox(width: 8),
                DefaultTextStyle(
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey[600],
                  ),
                  child: trailing ?? Icon(
                    Icons.chevron_right,
                    size: 20,
                    color: Colors.grey[350],
                  ),
                ),
              ],
            ),
          ),
        ),
        if (showDivider)
          Divider(
            height: 1,
            indent: 16,
            endIndent: 16,
            color: Colors.grey[100],
          ),
      ],
    );
  }

  /// 构建开关设置项
  Widget _buildSwitchItem({
    required String title,
    String? subtitle,
    required bool value,
    required ValueChanged<bool> onChanged,
    bool showDivider = true,
  }) {
    return _buildSettingItem(
      title: title,
      subtitle: subtitle,
      trailing: Switch.adaptive(  // 使用自适应样式
        value: value,
        onChanged: onChanged,
        activeColor: const Color(0xFFFF5722),
      ),
      showDivider: showDivider,
    );
  }

  /// 构建设置分组
  Widget _buildSettingGroup({
    required List<Widget> children,
  }) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        children: children,
      ),
    );
  }

  /// 退出登录
  Future<void> _logout(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.clear();  // 清除所有数据
    // 或者只清除登录相关数据
    // await prefs.remove('isLoggedIn');
    // await prefs.remove('token');
    // await prefs.remove('phone');
    
    if (!context.mounted) return;
    Navigator.pushReplacementNamed(context, '/login');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        title: const Text(
          '设置',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        children: [
          const SizedBox(height: 12),
          // 账号安全
          _buildSettingGroup(
            children: [
              _buildSettingItem(
                title: '账号与安全',
                subtitle: '实名认证、登录设置',
                onTap: () {
                  // TODO: 跳转到账号安全页面
                },
                showDivider: false,
              ),
            ],
          ),
          const SizedBox(height: 12),
          // 通知设置
          _buildSettingGroup(
            children: [
              _buildSwitchItem(
                title: '通知提醒',
                subtitle: '新订单、消息等通知',
                value: _notificationEnabled,
                onChanged: (value) {
                  setState(() {
                    _notificationEnabled = value;
                  });
                },
              ),
              _buildSwitchItem(
                title: '声音',
                value: _soundEnabled,
                onChanged: (value) {
                  setState(() {
                    _soundEnabled = value;
                  });
                },
              ),
              _buildSwitchItem(
                title: '震动',
                value: _vibrationEnabled,
                onChanged: (value) {
                  setState(() {
                    _vibrationEnabled = value;
                  });
                },
                showDivider: false,
              ),
            ],
          ),
          const SizedBox(height: 12),
          // 其他设置
          _buildSettingGroup(
            children: [
              _buildSettingItem(
                title: '隐私设置',
                onTap: () {
                  // TODO: 跳转到隐私设置页面
                },
              ),
              _buildSettingItem(
                title: '清除缓存',
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('12.6MB'),
                    const SizedBox(width: 4),
                    Icon(
                      Icons.chevron_right,
                      size: 20,
                      color: Colors.grey[350],
                    ),
                  ],
                ),
                onTap: () {
                  // TODO: 清除缓存
                },
              ),
              _buildSettingItem(
                title: '关于我们',
                onTap: () {
                  // TODO: 跳转到关于页面
                },
                showDivider: false,
              ),
            ],
          ),
          const SizedBox(height: 24),
          // 退出登录按钮
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: ElevatedButton(
              onPressed: () {
                showDialog(
                  context: context,
                  builder: (context) => CustomDialog(
                    title: '退出登录',
                    content: '退出后需要重新登录才能使用完整功能',
                    cancelText: '再想想',
                    confirmText: '退出登录',
                    confirmTextColor: Colors.red,
                    icon: Container(
                      width: 56,
                      height: 56,
                      decoration: BoxDecoration(
                        color: Colors.red[50],
                        shape: BoxShape.circle,
                      ),
                      child: const Icon(
                        Icons.logout_outlined,
                        color: Colors.red,
                        size: 28,
                      ),
                    ),
                    onConfirm: () => _logout(context),
                  ),
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.white,
                foregroundColor: Colors.red,
                elevation: 0,
                padding: const EdgeInsets.symmetric(vertical: 13),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: const Text(
                '退出登录',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          ),
          const SizedBox(height: 24),
        ],
      ),
    );
  }
} 